package com.atguigu.tingshu.search.api;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.search.AlbumInfoIndex;
import com.atguigu.tingshu.query.search.AlbumIndexQuery;
import com.atguigu.tingshu.search.service.SearchService;
import com.atguigu.tingshu.vo.search.AlbumInfoIndexVo;
import com.atguigu.tingshu.vo.search.AlbumSearchResponseVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "搜索专辑管理")
@RestController
@RequestMapping("api/search")
@SuppressWarnings({"all"})
public class SearchApiController {

    @Autowired
    private SearchService searchService;


    /**
     * 上架专辑-导入索引库
     * @param albumId
     * @return
     */
    @GetMapping("/albumInfo/upperAlbum/{albumId}")
    @Operation(summary = "上架专辑-导入索引库")
    public Result upperAlbum(@PathVariable(name = "albumId") Long albumId){

        searchService.upperAlbum(albumId);

        return Result.ok();
    }

    /**
     * 下架专辑-删除文档
     * @param albumId
     * @return
     */
    @GetMapping("/albumInfo/lowerAlbum/{albumId}")
    @Operation(summary = "下架专辑-删除文档")
    public Result lowerAlbum(@PathVariable(name = "albumId") Long albumId){
        searchService.lowerAlbum(albumId);

        return Result.ok();
    }

    /**
     * 站内搜索
     * @param albumIndexQuery
     * @return
     */
    @Operation(summary = "站内搜索")
    @PostMapping("/albumInfo")
    public Result<AlbumSearchResponseVo> searchAlbum(
            // 接收请求体参数，自动映射到AlbumSearchRequest对象
            @RequestBody AlbumIndexQuery albumIndexQuery
    ) {

        // 2. 调用业务层处理查询逻辑（示例）
        AlbumSearchResponseVo response = searchService.searchAlbum(albumIndexQuery);

        // 3. 返回统一格式的结果
        return Result.ok(response);
    }


    /**
     * 查询1级分类下置顶3级分类热度TOP6专辑
     * @param category1Id
     * @return [{"baseCategory3":{三级分类对象},list:[专辑列表]},,{其他6个置顶分类热门专辑Map}]
     */
    @Operation(summary = "查询指定一级分类下热门排行专辑")
    @GetMapping("/albumInfo/channel/{category1Id}")
    public Result<List<Map<String,Object>>> channel(@PathVariable Long category1Id){

        List<Map<String,Object>> mapList = searchService.channel(category1Id);

        return Result.ok(mapList);
    }

    /**
     * 根据用户已录入字符查询提词索引库进行自动补全关键字
     * @param keyword
     * @return
     */
    @Operation(summary = "根据用户已录入字符查询提词索引库进行自动补全关键字")
    @GetMapping("/albumInfo/completeSuggest/{keyword}")
    public Result<List<String>> completeSuggest(@PathVariable String keyword){
        List<String> list = searchService.completeSuggest(keyword);
        return Result.ok(list);
    }


    /**
     * 根据专辑ID汇总详情页所需参数
     *
     * @param albumId
     * @return
     */
    @Operation(summary = "查询专辑 详情")
    @GetMapping("/albumInfo/{albumId}")
    public Result<Map<String,Object>> albumInfo(@PathVariable Long albumId){

        Map<String,Object> map = searchService.albumInfo(albumId);

        return Result.ok(map);
    }


    /**
     * 更新Redis小时榜
     * @return
     */
    @Operation(summary = "更新所有分类下排行榜-手动调用")
    @GetMapping("/albumInfo/updateLatelyAlbumRanking/{top}")
    public Result updateLatelyAlbumRanking(@PathVariable Integer top){

        searchService.updateLatelyAlbumRanking(top);

        return Result.ok();
    }


    /**
     * 获取不同分类下不同排行维度TOP20
     * @param category1Id
     * @param dimension
     * @return
     */
    @Operation(summary = "获取不同分类下不同排行维度TOP20")
    @GetMapping("/albumInfo/findRankingList/{category1Id}/{dimension}")
    public Result<List<AlbumInfoIndexVo>> getRankingList(@PathVariable Long category1Id, @PathVariable String dimension) {
        List<AlbumInfoIndexVo> list = searchService.getRankingList(category1Id, dimension);
        return Result.ok(list);
    }

}

